package androidx.blogger;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.gdata.blogger.AppControl;
import androidx.gdata.blogger.AppDraft;
import androidx.gdata.blogger.BlogEntry;
import androidx.list.CellRenderer;
import androidx.view.ViewUtil;

public class BlogEntryRenderer implements CellRenderer<BlogEntry> {
  private Activity parent;

  public BlogEntryRenderer(Activity parent) {
    this.parent = parent;
  }

  public void applyDataToView(View view, BlogEntry data) {
    blogname_textview.setText(data.getTitle());
    if (isDraft(data)) {
      blogname_imageview.setVisibility(View.INVISIBLE);
    } else {
      blogname_imageview.setVisibility(View.VISIBLE);
    }
  }

  private static boolean isDraft(BlogEntry entry) {
    AppControl control = entry.getAppControl();
    if (control == null) {
      return false;
    }
    AppDraft draft = control.getDraft();
    if (draft == null || draft.getContent() == null) {
      return false;
    }
    return draft.getContent().equalsIgnoreCase("yes");
  }
  
  private ViewGroup blogLayout;
  private TextView blogname_textview;
  private ImageView blogname_imageview;

  public View createViewForData(Context context, BlogEntry data) {
    blogLayout = (ViewGroup) inflate(R.layout.blogentry_text);
    blogname_textview = (TextView) ViewUtil.findViewById(blogLayout, R.id.blogname_textview);
    blogname_imageview = (ImageView) ViewUtil.findViewById(blogLayout, R.id.blogname_imageview);
    blogname_imageview.setImageResource(R.drawable.android_blogger_mini_icon);
    return blogLayout;
  }

  public boolean isViewSuitableForData(View view, BlogEntry data) {
    return (view == blogLayout);
  }

  private View inflate(int resId) {
    return parent.getLayoutInflater().inflate(resId, null);
  }
}
